package UI.Table;

import javax.swing.JPanel;
import javax.swing.table.AbstractTableModel;

import Task.DSTask;

//@author A0094762W
/**
 * DSTaskDisplay holds the model that is used by DSUI to display the to-do tasks. 
 */
public class DSTaskDisplay extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	Object[] _rowData;
	int _rowNum, _columnNum;
	
	public DSTaskDisplay(Object[] rowData) {
		_rowData = rowData;
		
		_rowNum = rowData.length;
		_columnNum = 1;
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		return _rowData[rowIndex];
	}

	public int getRowCount() {
		return _rowNum;
	}
	
	public int getColumnCount() {
		return _columnNum;
	}
	
	public Class<?> getColumnClass(int columnIndex) {
		return JPanel.class;
	}
	
	public void setData(Object[] rowData) {
		_rowData = rowData;
		_rowNum = rowData.length;
		fireTableDataChanged();
	}
	
	public void refreshCell(int row) {
		fireTableCellUpdated(row, 0);
	}
	
	public void refreshTable() {
		fireTableDataChanged();
	}
	
	public boolean contains(DSTask task) {
		boolean hasTask = false;
		for (int i = 0; i < _rowData.length; i++) {
			String currTaskId = ((DSTask) _rowData[i]).getTaskId();
			if (currTaskId.equals(task.getTaskId())) {
				hasTask = true;
				i = _rowData.length;
			}
		}
		
		return hasTask;
	}
	
	public Object[] getTaskArray() {
		return _rowData;
	}
}
